package View;

import Controller.UserController;
import pojo.Car;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;
import java.awt.*;
import java.util.List;

public class VehicleModelDB extends AbstractTableModel {
    //Controller
    private UserController userController;

    String[] columnNames = new String[]{"ID","类型","品牌","车牌号","车辆状态","租金","燃油类型","颜色","里程数"};
    public static List<Car> cars;

    public VehicleModelDB(){
        try {
            userController = new UserController();
            //获取所有车辆信息
            cars = userController.getAllCars();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public VehicleModelDB(String brand){
        try {
            userController = new UserController();
            cars = userController.getAllCarsByBrand(brand);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 返回一共有多少行
    @Override
    public int getRowCount() {
        return cars.size();
    }

    // 返回一共有多少列
    @Override
    public int getColumnCount() {
        return columnNames.length;
    }
    // 获取每一列的名称
    @Override
    public String getColumnName(int columnIndex) {
        return columnNames[columnIndex];
    }
    // 单元格是否可以修改
    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;
    }
    // 每一个单元格里的值
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Car car =  cars.get(rowIndex);
        if (0 == columnIndex) {
            return car.getCar_id();
        }
        if (1 == columnIndex) {
            return car.getModel();
        }
        if (2 == columnIndex) {
            return car.getBrand();
        }
        if (3 == columnIndex) {
            return car.getLicense_plate();
        }
        if (4 == columnIndex) {
            return car.getStatus();
        }
        if (5 == columnIndex) {
            return car.getRental_rate();
        }
        if (6 == columnIndex) {
            return car.getFuel_type();
        }
        if (7 == columnIndex) {
            return car.getColor();
        }
        if (8 == columnIndex) {
            return car.getMileage();
        }
        return null;
    }

}
